package com.ys.controller.web.base;

import com.ys.config.MenuId;
import com.ys.entity.base.*;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工月度绩效信息
 */
@Controller
@RequestMapping("/web/base/tbUserMonthPerformance")
public class WebTbUserMonthPerformanceController {
	private static final Logger logger = LoggerFactory.getLogger(WebTbUserMonthPerformanceController.class);

	@Autowired
	private CommonApiService<TbUserMonthPerformance> commonApiService;


    public Map<String, Object> getEntity(Long id){
        Map<String,Object> entity = new TbUserMonthPerformance()
            .addLeftJoin("TS_UserInfo U ON U.UserId = A.UserId")//用户信息表
                .addField("(U.UserId + '/' + U.UserName) AS _ys_nameLinked")//工号/姓名

            .addLeftJoin("TS_OrganizationInfo OI ON OI.Orgid = U.DeptId")
                .addField("OI.DeptName AS deptName")//部门

            .addLeftJoin("THR_UserArchives UAS ON UAS.UserId = A.UserId")//用户档案表
                .addField("UAS.UserState AS userState")//员工状态

            .addLeftJoin("TS_OrganizationInfo O ON O.OrgId = A.OrgId")//组织机构表
                .addField("O.OrgName AS orgName")//所属公司

            //绩效申报状态
            .addLeftJoin("TB_UserMonthPerformanceReportItem RI ON RI.MonthPerformanceId = A.Id AND 1=1")
                .addLeftJoin("TB_UserMonthPerformanceReport R ON R.Bid = RI.Pbid")
                .addField("CASE " +
                        "WHEN RI.Id IS NOT NULL AND R.EffectStock = 1 THEN '已申报' " +
                        " ELSE '未申报' END reportStatus")//绩效申报状态
                /*
                .addField("RI.HrPerformancePoint AS hrPerformancePointByReport")//部门绩效评分
                .addField("RI.HrPerformanceContent AS hrPerformanceContentByReport")//部门评价内容
                .addField("RI.JobBonus AS jobBonus")//岗位提奖
                .addField("RI.JobBonusExplain AS jobBonusExplain")//岗位提奖说明
                 */
                .addField("CASE WHEN RI.Id IS NOT NULL AND R.EffectStock = 1 THEN RI.HrPerformancePoint ELSE NULL END  hrPerformancePointByReport")//部门绩效评分
                .addField("CASE WHEN RI.Id IS NOT NULL AND R.EffectStock = 1 THEN RI.HrPerformanceContent ELSE '' END hrPerformanceContentByReport")//部门评价内容
                //.addField("CASE WHEN RI.Id IS NOT NULL AND R.EffectStock = 1 THEN RI.JobBonus ELSE NULL END jobBonus")//岗位提奖
                .addField("CASE WHEN RI.Id IS NOT NULL AND R.EffectStock = 1 THEN RI.JobBonusExplain ELSE '' END jobBonusExplain")///岗位提奖说明


            //考勤状态
            .addLeftJoin("TB_UserAttendance UA ON A.UserAttendanceId = UA.Id AND 1=1")
                    .addField("CASE " +
                            "WHEN UA.Id IS NOT NULL THEN '已导入' " +
                            " ELSE '未导入' END attendanceImportStatus")//考勤状态
            //信息状态
            .addLeftJoin("TB_UserPaySlip PS ON PS.MonthPerformanceId = A.Id AND 1=1")
                    .addField("CASE " +
                            "WHEN PS.Id IS NOT NULL THEN '已生成' " +
                            " ELSE '未生成' END paySlipStatus")//信息状态

            .setId(id)
            .queryFirstMap();

        return entity;
    }


    public Map<String, Object> setExtraParam(Map<String,Object> entity){
        Map<String, Object> extraParam = new HashMap<String, Object>();
        //关联数据
        if(entity.get("_ys_nameLinked")!=null)extraParam.put("_ys_nameLinked", entity.get("_ys_nameLinked"));
        if(entity.get("deptName")!=null)extraParam.put("deptName", entity.get("deptName"));
        if(entity.get("userState")!=null)extraParam.put("userState", entity.get("userState"));
        if(entity.get("orgName")!=null)extraParam.put("orgName", entity.get("orgName"));
        if(entity.get("reportStatus")!=null)extraParam.put("reportStatus", entity.get("reportStatus"));
        if(entity.get("attendanceImportStatus")!=null)extraParam.put("attendanceImportStatus", entity.get("attendanceImportStatus"));
        if(entity.get("paySlipStatus")!=null)extraParam.put("paySlipStatus", entity.get("paySlipStatus"));
        if(entity.get("hrPerformancePointByReport")!=null)extraParam.put("hrPerformancePointByReport", entity.get("hrPerformancePointByReport"));
        if(entity.get("hrPerformanceContentByReport")!=null)extraParam.put("hrPerformanceContentByReport", entity.get("hrPerformanceContentByReport"));
        //if(entity.get("jobBonus")!=null)extraParam.put("jobBonus", entity.get("jobBonus"));
        if(entity.get("jobBonusExplain")!=null)extraParam.put("jobBonusExplain", entity.get("jobBonusExplain"));
        return extraParam;
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            Map<String,Object> entity = getEntity(id);
            Map<String, Object> extraParam = setExtraParam(entity);
            int isEnd = new TbUserPaySlip().setMonthPerformanceId(id).count();
            //int isRank = 1;
            int isSoseBase = new TbSocialSecurityPayItem().setUserMonthPerformanceId(id).count();
            int isMiBase = new TbMedicarePayItem().setUserMonthPerformanceId(id).count();
            int isHousingPfBase = new TbReservedPayItem().setUserMonthPerformanceId(id).count();

            //model.addAttribute("isRank", isRank);
            model.addAttribute("isSoseBase", isSoseBase>0?1:0);
            model.addAttribute("isMiBase", isMiBase>0?1:0);
            model.addAttribute("isHousingPfBase", isHousingPfBase>0?1:0);

            extraParam.put("isSoseBase", isSoseBase>0?1:0);
            extraParam.put("isMiBase", isMiBase>0?1:0);
            extraParam.put("isHousingPfBase", isHousingPfBase>0?1:0);

            //已生成工资条的不能编辑
            if(isEnd>0){
                model.addAttribute("onProcess", 1);
                commonApiService.toDetail(model,id, new TbUserMonthPerformance(), MenuId.TB_UserMonthPerformance, extraParam, null);
            }else{
                model.addAttribute("onProcess", 0);
                commonApiService.toEdit(model,id, new TbUserMonthPerformance(), MenuId.TB_UserMonthPerformance, extraParam, null);
            }
            return "base/tbUserMonthPerformance/toEdit";
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            Map<String,Object> entity = getEntity(id);
            Map<String, Object> extraParam = setExtraParam(entity);
            model.addAttribute("onProcess", 1);
            return commonApiService.toDetail(model,id, new TbUserMonthPerformance(), MenuId.TB_UserMonthPerformance, extraParam, null);
            //return "base/tbUserMonthPerformance/toEdit";
        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }




}






